package br.edu.sistemaacademico;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import br.edu.sistemaacademico.facade.DataFacade;
import br.edu.sistemaacademico.pojo.Classe;
import br.edu.sistemaacademico.pojo.Disciplina;
import br.edu.sistemaacademico.pojo.Professor;
import br.edu.sistemaacademico.pojo.Turma;
import br.edu.sistemaacademico.state.StateHandler;
import client.ClientCommand;

/**
 *
 * @author baron
 */
public class frmTurma extends FrmModelo{
	Turma turma;
	StateHandler stateAlterarExcuir = new StateHandler(false);
	public List<Object> campos = ClientCommand.getCampos("TurmaService"); //new ArrayList<Object>();
    private List<Object> valores = new ArrayList<Object>();
    private List<Object> tamanho = new ArrayList<Object>();
    TabelaModelo tabModel;
    List<Turma> extra;
    
    public frmTurma(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        tamanho.add("70");
        tabModel = new TabelaModelo(campos, tamanho, getJTable());
        ListSelectionModel listModel = getJTable().getSelectionModel();
        listModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        listModel.addListSelectionListener(list);
        tabModel.busca(getTfBuscar());
        setTitle("Turma");
        getJbtnGravar().setActionCommand("gravar");
        getJbtnGravar().addActionListener(listener);
        getbtnVoltar().addActionListener(listener);
        getbtnVoltar().setActionCommand("voltar");
        getbtnExcluir().setActionCommand("excluir");
        getbtnExcluir().addActionListener(listener);
        getbtnAlterar().setActionCommand("alterar");
        getbtnAlterar().addActionListener(listener);
        setButtons();
        setStateAlteraExcuir();
        atualiza();
        setVisible(true);
    }

    public void setButtons(){
        JButton btn4 = getbtn4();
        JButton btn5 = getbtn5();
        JButton btn6 = getbtn6();

        btn4.setVisible(false);
        btn5.setVisible(false);
        btn6.setVisible(false);
    }
  
	
	
	public void atualiza(){
		((DefaultTableModel) getJTable().getModel()).setNumRows(0);
		getJTable().updateUI();
		this.extra = (List) ClientCommand.carregar("TurmaService","codigo = codigo", "");
		DataFacade entrada = new DataFacade();   
    	for(int i = 0; i < extra.size();i++ )
    		if(extra.get(i).getClasse().getId()!=null){
    		valores = new LinkedList<Object>();
    		valores.add(extra.get(i).getId());
    		valores.add(extra.get(i).getTamanho());
    		valores.add(extra.get(i).getPeriodo());
    		extra.get(i).setProfessor((Professor) ClientCommand.carregaObjeto("ProfessorService"," codigo ="+extra.get(i).getProfessor().getId(), ""));
			extra.get(i).setClasse((Classe) ClientCommand.carregaObjeto("ClasseService","codigo ="+extra.get(i).getClasse().getId(),""));
			extra.get(i).getClasse().setDisicplina((Disciplina) ClientCommand.carregaObjeto("DisciplinaService","codigo ="+extra.get(i).getClasse().getDisicplina().getId(),""));
    		valores.add(extra.get(i).getProfessor().getNome());
    		valores.add(extra.get(i).getClasse().getDisicplina().getNome());
    		tabModel.setValores(valores);
    		valores.clear();
    		valores.removeAll(extra);
    	}
	}
ActionListener listener = new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			//if(e.getActionCommand().equals("excluir"));
				//professorDao.remover(o);
			if(e.getActionCommand().equals("voltar"))
			dispose();
			else if(e.getActionCommand().equals("excluir"))
				remover();
			else if(e.getActionCommand().equals("alterar"))
				 new frmAtualizaTurma(turma);
			else if(e.getActionCommand().equals("gravar"))
				new frmCadastroTurma();
			
				atualiza();
			
		}
	};
    ListSelectionListener list = new ListSelectionListener() {
		
		@Override
		public void valueChanged(ListSelectionEvent e) {
			try{
			turma = getSelectedTurma(Long.parseLong(getJTable().getValueAt(getJTable().getSelectedRow(), 0).toString()));
			stateAlterarExcuir.change(true);
			}catch(ArrayIndexOutOfBoundsException ea){
				turma = null;
				stateAlterarExcuir.change(false);
			}
		}
	};	
	
	public Turma getSelectedTurma(Long codigo){
		return (Turma) ClientCommand.carregaObjeto("TurmaService", "codigo ="+codigo, "");
	}
	public void remover(){
		ClientCommand.deletar("TurmaService",turma);
		atualiza();
	}
    public void setStateAlteraExcuir(){
    	stateAlterarExcuir.addComponent(getbtnAlterar());
    	stateAlterarExcuir.addComponent(getbtnExcluir());
    }
}
